#!/bin/bash
# ~/Desktop/workspaces/python-tool/shell/tts-generate.sh

# array=(
#   "已搜索到车位，请停车"
#   )

# array=(
#   "请系好安全带"
#   "请关好车门"
#   "正在连接手机，请打开ARCFOX极狐手机应用继续"
#   )

array=(
"主驾请讲"
"副驾请讲"
"右后请讲"
"哈喽，小狸来也"
"嘿，你叫我吗？"
"在呢"
"左后请讲"
"怎么啦？需要小狸的帮忙吗？"
"您好呀，小狸在岗，伴您安全出行"
"我在"
"我在听"
"我是小狸哦，很高兴和你说话"
"我来了"
"旅途不孤单，小狸的声音伴随你"
"请吩咐"
"驾临愉快！小狸为你保驾护航"
)

adb shell rm -rf /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
adb shell mkdir -p /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
adb shell rm -rf /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache2
adb shell mkdir -p /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache2

for ((i = 0; i < ${#array[@]}; i++)); do
  echo "##########################################"
  echo "生成 ${array[i]}，start"

  adb shell  am broadcast -a  com.aispeech.lyra.action --es key speak --es val ${array[i]} -f 0x01000000
  sleep 8

  # 查找所有 mp3 文件并获取最后一个
  last_mp3=$(adb shell find /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/ -name "*.mp3" -type f | tail -n 1)

  # 检查是否找到了 mp3 文件
  if [ -z "$last_mp3" ]; then
      echo "未找到 mp3 文件"
      exit 1
  else
      # 获取文件名（不含路径）
      filename=$(basename "$last_mp3")
      echo "最后一个 mp3 文件: $filename"
  fi

  # adb shell mv $target_file /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache2/${array[i]}.mp3
  # # adb shell rm -rf /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/${array[i]}.mp3
  # target_file=$(ls /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/[0-9]*.mp3)
  if ! adb shell mv $last_mp3 /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache2/${array[i]}.mp3; then
    echo ${array[i]}
  fi
  
  adb shell rm -rf /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
  sleep 1
  adb shell mkdir -p /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
  sleep 1

  # if [ -n "$target_file" ] && [ -f "$target_file" ]; then
  #   adb shell mv $target_file /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache2/${array[i]}.mp3
  #   adb shell rm -rf /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
  #   adb shell mkdir -p /sdcard/Android/data/com.aispeech.lyra.daemon/cache/ttsCache/
  # else
  #   # 没有找到文件，打印123.MP3
  #   echo "$target_file"
  # fi
  echo "生成 ${array[i]}，end"
  echo "##########################################"
done
  
